package com.hongsen.cn.Utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
 * 作者：Mio
 * 时间：2019/07/24.
 * 邮箱：17379235050@163.com
 * 描述: GsonUtils 工具类
 */
public class GsonUtils {

    private static final GsonUtils ourInstance = new GsonUtils();
    private final Gson sGson;

    public static GsonUtils getInstance() {
        return ourInstance;
    }

    private GsonUtils() {
        sGson = new Gson();
    }

    public Gson getsGson() {
        Gson localGson = sGson;
        if (localGson == null) {
            localGson = new Gson();
        }
        return localGson;
    }

    public <T> T fromJson(String json, Class<T> classOfT) {
        try {
            return getsGson().fromJson(json, classOfT);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            Log.i("cl", "json数据解析失败 ");
        }

        return null;
    }


    public String toJson(Object src) {
        try {
            return getsGson().toJson(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
